import os
from base_server.sqlop import *
from test_code.to_xls import *


class Qa_addtask(SqlOperate):
	#连接数据库
	def __init__(self):
		conf = configparser.ConfigParser()
		conf.read("conf/config.ini")
		self.host = conf.get('qa','host')
		self.user = conf.get('qa','user')
		self.passwd = conf.get('qa','passwd')
		self.database = conf.get('qa','database')

	#文件上传
	def fileupload(self,f):
		way = ""
		if(platform.system()=='Windows'):
			way = "C:/Users/ST/Desktop/check_point/tmp/"
		elif(platform.system()=='Linux'):
			way = "/data/logs/cron/"
		basepath = os.path.dirname(__file__)  # 当前文件所在路径
		upload_path = os.path.join(basepath, way,f.filename)#secure_filename(f.filename)
		upload_path = os.path.abspath(upload_path) # 将路径转换为绝对路径
		f.save(upload_path)

	#新增
	def add_task(self,name,content):
		result = self.iftask(name)
		self.dbcur()
		sql = ""
		if result:
			sql = 'update addtask SET taskname="%s",begintime="%s",addtime=curdate() WHERE taskname="%s"'%(name,content,name)
		else:
			sql = 'INSERT INTO addtask (taskname,begintime,addtime,type)VALUES( "%s","%s",curdate(),0)'%(name,content)
		self.sqlExe(sql)
		self.sqlCom()
		self.sqlclo()
		return "pass"

	#判断新增数据是否存在
	def iftask(self,name):
		self.dbcur()
		sql = 'SELECT taskname FROM addtask WHERE taskname ="%s"'%name
		self.sqlExe(sql)
		self.sqlCom()
		self.sqlclo()
		data = self.cur.fetchall()
		return data

	#查询
	def sel_task(self):
		self.dbcur()
		sql = 'select * from addtask'
		self.sqlExe(sql)
		self.sqlCom()
		self.sqlclo()
		data = self.cur.fetchall()
		result=list(data)
		for c in result:
			result[result.index(c)] = list(c)
		for i in result:
			if i[4]==0:
				i[4]='启动'
			else:
				i[4]='停止'
		# print(result)
		return result
	#删除
	def del_task(self,ID):
		self.dbcur()
		sql = 'delete from addtask where id = "%s"'%ID
		self.sqlExe(sql)
		self.sqlCom()
		self.sqlclo()
		return "pass"

	#修改
	def edit_task(self,id,name,content):
		self.dbcur()
		sql = 'update addtask SET taskname="%s",begintime="%s",addtime=curdate() WHERE id="%s"'%(name,content,id)
		self.sqlExe(sql)
		self.sqlCom()
		self.sqlclo()
		return 'pass'

	#添加任务
	def ttask_add(self, task_cron):
		# 本地测试地址：C:/Users/ST/Desktop/ABC/f/a.txt
		with open("/var/spool/cron/root", "r", encoding="utf-8") as f:
			lines = f.readlines()
		with open("/var/spool/cron/root", "a", encoding="utf-8") as f_w:
			for line in lines:
				if task_cron in line:
					ret = "任务已存在"
					break
			else:
				f_w.write(task_cron+'\n')
				ret = "添加定时任务成功"
			return ret

	#删除任务
	def ttask_del(self, task_cron):
		with open("/var/spool/cron/root", "r", encoding="utf-8") as f:
			lines = f.readlines()
		with open("/var/spool/cron/root", "w", encoding="utf-8") as f_w:
			for line in lines:
				if task_cron in line:
					continue
				f_w.write(line)
	# 启动停止任务
	def run(self,status,task_cron,id):
		self.dbcur()
		if status == "启动":
			self.ttask_add(task_cron)
			sql = 'update addtask SET type="1" WHERE id="%s"'%id
		elif status == "停止":
			self.ttask_del(task_cron)
			sql = 'update addtask SET type="0" WHERE id="%s"'%id
		self.sqlExe(sql)
		self.sqlCom()
		self.sqlclo()
		return "pass"